//
//  TBLFile.m
//  MapEdit
//
//  Created by Steven Canfield on 28/06/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "TBLFile.h"


@implementation TBLFile
- (id)initWithData:(NSData *)data {
	int dataIndex = 0;
	
	u_int16_t stringCount;
	
	[data getBytes:&stringCount range:NSMakeRange( dataIndex, sizeof( u_int16_t ) )];
	dataIndex += sizeof( u_int16_t );
	stringCount = NSSwapLittleShortToHost( stringCount );
	
	strings = [[NSMutableArray alloc] init];

	char * dataBytes = (char *)[data bytes];
	
	int i = 0;
	while( stringCount-- ) {
		u_int16_t offset;
		[data getBytes:&offset range:NSMakeRange( dataIndex , sizeof( u_int16_t ))];
		offset = NSSwapLittleShortToHost( offset );
		dataIndex += sizeof( u_int16_t );
		NSString * buffer = [NSString stringWithCString:&dataBytes[ offset ] encoding:NSASCIIStringEncoding];
		[strings addObject:buffer];
		i++;
	}
	
	NSLog(@"Loaded %i Strings", [strings count] );
	
	return self;
}

- (NSString *)stringAtIndex:(int)index {
	return [strings objectAtIndex:index];
}

- (int)stringCount {
	return [strings count];
}
@end
